setwd("../")
rm(list = ls())

# # only need to install once
# devtools::install_github("cloudyr/pyMTurkR", dependencies=TRUE)
# devtools::install_github("Luwei-Ying/validateIt", dependencies=TRUE)
# load R package
library(validateIt)

### load documents with predicted topics
top1domText <- read.csv("textpool/top1domText.csv", stringsAsFactors = FALSE)

OLnarrowcursory <- validateLabel(type = "OL", n = 500, 
                                text.predict = top1domText, text.name = "post_text",
                                labels = c("Working Class",
                                           "Planned Parenthood",
                                           "Farm Bill",
                                           "Economy",
                                           "Prescription Medicine",
                                           "Grants for Colleges",
                                           "Banking",
                                           "Government Spending",
                                           "Healthcare",
                                           "Debt Ceiling"), 
                                labels.index = c(1, 9, 21, 35, 44, 62, 65, 70, 88, 89), 
                                labels.add = NULL)

goldOL <- read.csv("goldstandard/goldOL.csv", stringsAsFactors = FALSE)
OLnarrowcursory <- mixGold(tasks = OLnarrowcursory, golds = goldOL)
OLnarrowcursory <- recordTasks(type = "OL", tasks = OLnarrowcursory,
                              path = "labelrecords/OLnarrowcursory.Rdata")

# ##### interact with Mturk from here #####
# ### environment
# library(pyMTurkR)
# Sys.setenv(AWS_ACCESS_KEY_ID = "AWS_ACCESS_KEY_ID")
# Sys.setenv(AWS_SECRET_ACCESS_KEY = "AWS_SECRET_ACCESS_KEY")
# options(pyMTurkR.sandbox = T) # Change sandbox = F when ready to run on MTurk
# AccountBalance()
# 
# ### send tasks (first trial)
# OLnarrowcursoryids <- sendTasks(hit_type = 'find_from_dashboard', hit_layout = 'find_from_dashboard', type = "OL",
#                                tasksrecord = OLnarrowcursory, HITidspath = "HITids/OLnarrowcursoryids1.Rdata")
# ### extend HITs if needed
# for(i in OLnarrowcursoryids[[1]]){
#   ExtendHIT(hit = i, add.seconds = 60*60*26)
# }
# 
# ### get results
# load("HITids/OLnarrowcursoryids1.Rdata")
# OLnarrowcursoryresults1 <- getResults(batch_id = "OLnarrowcursory1", hit_ids = HITids, retry = FALSE)
# write.csv(OLnarrowcursoryresults1, "OL/results/OLnarrowcursoryresults1.csv", row.names = FALSE)
# evalResults(results = OLnarrowcursoryresults1, key = OLnarrowcursory, type = "OL")
# 
# ### send tasks (second trial)
# OLnarrowcursoryids <- sendTasks(hit_type = 'find_from_dashboard', hit_layout = 'find_from_dashboard', type = "OL",
#                                tasksrecord = OLnarrowcursory, HITidspath = "HITids/OLnarrowcursoryids2.Rdata")
# ### extend HITs if needed
# for(i in OLnarrowcursoryids[[1]]){
#   ExtendHIT(hit = i, add.seconds = 60*60*26)
# }
# 
# ### get results
# load("HITids/OLnarrowcursoryids2.Rdata")
# OLnarrowcursoryresults2 <- getResults(batch_id = "OLnarrowcursory2", hit_ids = HITids, retry = FALSE)
# write.csv(OLnarrowcursoryresults2, "OL/results/OLnarrowcursoryresults2.csv", row.names = FALSE)
# evalResults(results = OLnarrowcursoryresults2, key = OLnarrowcursory, type = "OL")